<?php
/**
 * 权限管理
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-6-17
 * Time: 下午2:37
 */
namespace app\modules\content\controllers;

use app\modules\content\models\Book;
use yii;
use app\libs\ApiControl;
use app\libs\GetData;

class BookController extends ApiControl
{
    public $enableCsrfValidation = false;

    public function actionIndex()
    {
        $data = Yii::$app->db->createCommand("select * from {{%book}}")->queryAll();
        return $this->render('index', ['data' => $data]);
    }

    public function actionAdd()
    {
        if (!$_POST) {
            $id = Yii::$app->request->get('id', '');
            if (empty($id)) {
                return $this->render('add');
            } else {
                $data = Yii::$app->db->createCommand("select * from {{%book}} where id=" . $id)->queryOne();
                return $this->render('add', ['data' => $data]);
            }

        } else {
            $book = new Book();
            $getdata = new GetData();
            $must = array('pic' => '图片');
            $data = $getdata->PostData($must, 'book');
            if (empty($data['id'])) {
                $re = Yii::$app->db->createCommand()->insert("{{%book}}", $data)->execute();
            } else {
                $re = $book->updateAll($data, 'id=:id', array(':id' => $data['id']));
            }
            if ($re) {
                $this->redirect('index');
            } else {
                echo '<script>alert("数据修改/添加失败，请重试");history.go(-1);</script>';
                die;
            }
        }

    }

    public function actionDel()
    {
        $id = Yii::$app->request->get('id', '');
        $re = Book::deleteAll("id=:id", array(':id' => $id));
        if ($re) {
            echo true;
        }

    }
}